package edu.ipfw.cs590.cheat.services.mgmt.handlers;
/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

import java.util.Collections;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

 
/**
 *
 * @author rrm46582
 */
public class NewMessageHandler implements SOAPHandler<SOAPMessageContext> {

    public boolean handleMessage(SOAPMessageContext messageContext)
    {
           
        try
        {
            if ( (Boolean)messageContext.get(messageContext.MESSAGE_OUTBOUND_PROPERTY))
            {
                if ( messageContext.containsKey("FAULT"))
                {
                    String faultMessage = (String) messageContext.get("FAULT");
                    SOAPMessage msg = messageContext.getMessage();
//                ServletOutputStream os = response.getOutputStream();


                //Get the headers in order to build the SOAP Message
//                MimeHeaders mimeHeaders = new MimeHeaders();

                    MessageFactory messageFactory = MessageFactory.newInstance();
                    SOAPMessage message = messageFactory.createMessage(/*mimeHeaders, request.getInputStream()*/);
                    SOAPBody body = message.getSOAPBody();
                    body.addFault(new QName("ASM_FAULT"), faultMessage);

                    messageContext.setMessage(message);
                }
                return true;
/*
                //Time to responsd, let's create an output message
                SOAPMessage outMessage = messageFactory.createMessage();
                outMessage.getSOAPHeader().detachNode();
            	SOAPBody body = outMessage.getSOAPBody();


                //Let's put some reply text into the body
                SOAPFactory soapFactory = SOAPFactory.newInstance();
                Name name = soapFactory.createName("result");
                SOAPElement symbol = body.addBodyElement(name);
                symbol.addTextNode("success");
                outMessage.saveChanges();

                //Set the reponse headers and send the message by writing to the
                //Servlet output stream
                response.setHeader("Content-Type", "text/xml");
                outMessage.writeTo(os);
                return true;
  */
            }
            
            SOAPMessage msg = messageContext.getMessage();

           // msg.writeTo(System.out);
       //     SOAPHeader hdr = msg.getSOAPHeader();
            //messageContext.setMessage(msg);
       
       //if (hdr == null)
         //   {
          //      System.out.println("Null!");
           //     return false;
            //}

       // NodeList nodes = hdr.getElementsByTagNameNS("HTTP://cheat.cs590.ipfw.edu", "token");
        //if ( nodes.getLength() > 0)
       // {
        //    String token = nodes.item(0).getTextContent();
         //   System.out.println(token);
          //  if ( token.compareToIgnoreCase("HACK ATTACK") == 0)
           //     return false;
       // }
       // else
       // {
        //    System.out.println("No hdr elements");
        //    return false;
       // }
        System.out.println("Handler!!!");
        return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
        
    }

    public Set<QName> getHeaders() {
        return Collections.EMPTY_SET;
    }

    public boolean handleFault(SOAPMessageContext messageContext) {
        return true;
    }

    public void close(MessageContext context) {
    }

}
